package com.shop.shoppingmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.shop.shoppingmall.dao.ShDayCoinDao;
import com.shop.shoppingmall.entity.ShDayCoin;
import com.shop.shoppingmall.service.ShDayCoinService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 馨天币明细(ShDayCoin)表服务实现类
 *
 * @author makejava
 * @since 2020-09-11 10:59:03
 */
@Service("shDayCoinService")
@Transactional
public class ShDayCoinServiceImpl implements ShDayCoinService {
    @Resource
    private ShDayCoinDao shDayCoinDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShDayCoin queryById(String id) {
        return this.shDayCoinDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param shDayCoin 实例对象
     * @return 实例对象
     */
    @Override
    public ShDayCoin insert(ShDayCoin shDayCoin) {
        this.shDayCoinDao.insert(shDayCoin);
        return shDayCoin;
    }

    /**
     * 修改数据
     *
     * @param shDayCoin 实例对象
     * @return 实例对象
     */
    @Override
    public ShDayCoin update(ShDayCoin shDayCoin) {
        this.shDayCoinDao.update(shDayCoin);
        return this.queryById(shDayCoin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shDayCoinDao.deleteById(id) > 0;
    }

    /**
     * 馨天币表的支入明细
     * @param userId
     * @param divide
     * @param detail
     * @return
     */
    @Override
    public Result insertShCoin(String userId, BigDecimal divide,String detail) {
        String id = UUIDGenerator.generate();
        ShDayCoin shDayCoin = new ShDayCoin();
        shDayCoin.setId(id);
        shDayCoin.setUserId(userId);
        shDayCoin.setIncome(divide);
        shDayCoin.setType("1");
        shDayCoin.setDetail(detail);
        this.shDayCoinDao.insert(shDayCoin);
        return Result.ok();
    }

    /**
     * 馨天币支出
     * @param userId 用户id
     * @param divide 支出金额
     * @param detail  详情
     * @return
     */
    @Override
    public Result insertShCoinExpenditure(String userId, BigDecimal divide,String detail) {
        String id = UUIDGenerator.generate();
        ShDayCoin shDayCoin = new ShDayCoin();
        shDayCoin.setId(id);
        shDayCoin.setUserId(userId);
        shDayCoin.setExpenditure(divide);
        shDayCoin.setType("2");
        shDayCoin.setDetail(detail);
        this.shDayCoinDao.insert(shDayCoin);
        return Result.ok();
    }

    @Override
    public Result queryAll(Map map) {
        List<ShDayCoin> dayCoinList = shDayCoinDao.queryAll(map);
        return Result.ok(dayCoinList);

    }

    @Override
    public Result queryDayCoinAll(Map map) {
        Integer pagenum = (Integer) map.get("pagenum");
        Integer pagesize = (Integer) map.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        int a = (pagenum-1)*pagesize;
        map.put("offset",a);
        map.put("limit",pagesize);
        List<ShDayCoin> shDayCoins = shDayCoinDao.queryDayCoinAll(map);
        if(shDayCoins == null){
            return Result.error("暂时还没有馨天币明细的信息！");
        }
        Map map1 = new HashMap();
        ShDayCoin shDayCoin = shDayCoinDao.selectCountAll(map);

        map1.put("total", shDayCoin.getSums());
        map1.put("shDayCoins",shDayCoins);
        return Result.ok(map1);
    }
}
